Consider the following recursive C function.void get(int n){if (n<...
T(n) = T(n-1) + T(n-3) + 2, here T(n) denotes the number of times a recursive call is made for input n. 2 denotes the two direct recursive calls.
T(n<=0) = 0
T(1) = 2
T(2) = 4
T(3) = 6
T(4) = 10
T(5) = 16
T(6) = 24
So, answer is 24 + 1 call from main = 25.
View all questions of this test
Consider the following recursive C function.void get(int n){if (n<...
The given recursive C function `get(int n)` prints the value of `n` in a specific order. Let's break down the function and analyze its behavior to determine how many times it will be invoked before returning to `main()` when `get(6)` is called.
Recursive Function Analysis:
1. If `n` is equal to 1, the function immediately returns.
2. Otherwise, the function calls itself twice with different parameters:
a. `get(n-1)`
b. `get(n-3)`
3. Finally, the function prints the value of `n`.
Function Invocation Analysis:
1. The initial call is `get(6)` from `main()`. So, the function is invoked for the first time.
2. Inside `get(6)`, the first recursive call is `get(6-1)`, which means `get(5)` is invoked.
3. Inside `get(5)`, the first recursive call is `get(5-1)`, which means `get(4)` is invoked.
4. Inside `get(4)`, the first recursive call is `get(4-1)`, which means `get(3)` is invoked.
5. Inside `get(3)`, the first recursive call is `get(3-1)`, which means `get(2)` is invoked.
6. Inside `get(2)`, the first recursive call is `get(2-1)`, which means `get(1)` is invoked.
7. Inside `get(1)`, the function immediately returns without any further recursive calls.
8. Now, the control goes back to the previous stack frame, which was `get(2)`.
9. Inside `get(2)`, the second recursive call is `get(2-3)`, which means `get(-1)` is invoked. However, since `n` is negative, this call does not execute any further recursive calls or printing.
10. The control goes back to the previous stack frame, which was `get(3)`.
11. Inside `get(3)`, the second recursive call is `get(3-3)`, which means `get(0)` is invoked.
12. Inside `get(0)`, the function immediately returns without any further recursive calls.
13. The control goes back to the previous stack frame, which was `get(3)`.
14. Finally, inside `get(3)`, the function prints the value 3.
15. The control goes back to the previous stack frame, which was `get(4)`.
16. Inside `get(4)`, the second recursive call is `get(4-3)`, which means `get(1)` is invoked.
17. Inside `get(1)`, the function immediately returns without any further recursive calls.
18. The control goes back to the previous stack frame, which was `get(4)`.
19. Finally, inside `get(4)`, the function prints the value 4.
20. The control goes back to the previous stack frame, which was `get(5)`.
21. Inside `get(5)`, the second recursive call is `get(5-3)`, which means `get(2)` is invoked.
22. Inside `get(2)`, the first recursive call is `get(2-1)`,